﻿using System.Net;
using System.Net.Mail;
using MuzoBlog.Core.BussinessLogic.Settings;

namespace MuzoBlog.Core.BussinessLogic.Email
{
    public abstract class SmtpClientFactory
    {
        /// <summary>
        /// Creates smtp client from setting information
        /// </summary>
        /// <returns></returns>
        public static SmtpClient CreateSmtpClient()
        {
            return CreateSmtpClient(
                    SettingManager.GetStringValue("EmailConfiguration.Server"),
                    SettingManager.GetIntegerValue("EmailConfiguration.Port"),
                    SettingManager.GetStringValue("EmailConfiguration.UserName"),
                    SettingManager.GetStringValue("EmailConfiguration.Password"),
                    SettingManager.GetBooleanValue("EmailConfiguration.EnableSsl")
                );
        }

        public static SmtpClient CreateSmtpClient(string server, int port, string userName, string password, bool enableSsl)
        {
            SmtpClient smtpClient = new SmtpClient(server, port);
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                smtpClient.UseDefaultCredentials = false;
                smtpClient.Credentials = new NetworkCredential(userName, password);
            }

            smtpClient.EnableSsl = enableSsl;

            return smtpClient;
        }
    }
}